<template>
	<view class="ass-page">
		<view class="content">
			<view class="box">
				<view v-if="info.status==1">
					<text class="status_tip">商家处理中</text>
					<!-- 动态倒计时提示 -->
					<view class="countdown-section">
						<uv-count-down :time="remainingTime" format="DD:HH:mm:ss" autoStart millisecond
							@change="onChange" @finish="onFinish">
							<view class="time">
								<text class="time__item">{{ timeData.days }}&nbsp;天</text>
								<text
									class="time__item">{{ timeData.hours>10?timeData.hours:'0'+timeData.hours}}&nbsp;时</text>
								<text class="time__item">{{ timeData.minutes }}&nbsp;分</text>
								<text class="time__item">{{ timeData.seconds }}&nbsp;秒</text>
							</view>
						</uv-count-down>
						<text class="countdown-text">后商家未处理,系统将自动退款</text>
					</view>
				</view>
				<block v-if="info.status==1 || info.status ==3">
					<uv-line color="#EEEEEE" />
					<uv-gap height="10" bgColor="transparent" />
					<view class="brand">
						<view class="brand_name">
							<uv-icon :name="info.brand_logo" size="20" />
							<text class="f24">{{info.merchant_name}}</text>
						</view>
						<view class="brand_name">
							<uv-icon name="chat" color="#ff0000" size="20" />
							<text class="f24">联系商家</text>
						</view>
					</view>
					<view class="tips">
						“亲亲，我们已经收到您的申请，会在第一时间为您处理，请您耐心等待”
					</view>
					<view class="padding2" v-if="info.status==3">
						<button type="button" class="btn_comm">去和商家沟通</button>
					</view>
				</block>
				<view v-else-if="info.status==2 || info.status==5">
					<text class="status_tip">退款成功</text>
					<view class="price_row">
						<text class="pay_val">￥<text class="price">{{info.refund_price}}</text></text>
					</view>
					<uv-steps :current="0" dot active-color="#4bc3a7">
						<uv-steps-item title="同意退款" desc=""></uv-steps-item>
						<uv-steps-item title="平台处理中"></uv-steps-item>
						<uv-steps-item title="到账成功"></uv-steps-item>
					</uv-steps>
				</view>
				<view v-else-if="info.status==3">
					<text class="status_tip">商家拒绝申请</text>
					<view class="countdown-section">
						<uv-count-down :time="remainingTime" format="DD:HH:mm:ss" autoStart millisecond
							@change="onChange" @finish="onFinish">
							<view class="time">
								<text class="time__item">{{ timeData.days }}&nbsp;天</text>
								<text
									class="time__item">{{ timeData.hours>10?timeData.hours:'0'+timeData.hours}}&nbsp;时</text>
								<text class="time__item">{{ timeData.minutes }}&nbsp;分</text>
								<text class="time__item">{{ timeData.seconds }}&nbsp;秒</text>
							</view>
						</uv-count-down>
						<text class="countdown-text">后未处理,系统将自动关闭此次售后</text>
					</view>
				</view>
				<view v-else-if="info.status == 4">
					<text class="status_tip">申请已取消</text>
					<text class="countdown-text">若您的问题未解决，可在有效期内再次申请售后</text>
				</view>
			</view>
			<uv-gap height="10" bgColor="transparent" />
			<view class="box">
				<view class="brand">
					<text class="f24 f600">协商记录</text>
					<view class="flex">
						<text class="f24 c97">您可添加留言/上传凭证</text>
						<uv-icon name="arrow-right" size="12" />
					</view>
				</view>
			</view>
			<block v-if="info.refund_express">
				<uv-gap height="10" bgColor="transparent" />
				<view class="box">
					<view class="pay_row">
						<text class="pay_lbl">退货运费</text>
						<text class="pay_val">￥<text class="price1">{{info.refund_express_prices}}</text></text>
					</view>
					<view class="pay_row" @click="goLogistics">
						<text class="pay_lbl">退货物流：<text class="c333">{{info.refund_express_name}} {{info.refund_express}}</text></text>
						<uv-icon name="arrow-right" size="12" color="#979797" />
					</view>
				</view>
			</block>
			
			<uv-gap height="10" bgColor="transparent" />
			<view class="box">
				<view class="flex">
					<image :src="info.product_image" class="thumb" />
					<view class="flex-1 flex align-start justify-between">
						<view class="skus">
							<text class="pName">{{info.product_name}}</text>
							<uv-gap height="5" bgColor="transparent" v-if="info.spec_values" />
							<view class="tags" v-if="info.spec_values">{{info.spec_values}}</view>
						</view>
						<view class="jiage">
							<view class="t1">￥{{info.product_price}}</view>
							<view class="t2">x{{info.cart_num}}</view>
						</view>
					</view>
				</view>
				<view class="order-info">
					<view class="orderNo">
						<view class="lbl_pay">订单编号：</view>{{info.order_no}}
					</view>
					<uv-button size="mini" type="primary" plain @click="copyOrderNo" style="margin-left: 10px;">
						复制
					</uv-button>
				</view>
				<view class="order-info1">
					<view class="lbl_pay">申请金额：</view>
					<view class="pay_info">￥{{info.refund_price}}</view>
				</view>
				<view class="order-info1">
					<view class="lbl_pay">退款原因：</view>
					<view class="pay_info">{{info.apply_for_reason_name}}</view>
				</view>
				<view class="order-info1">
					<view class="lbl_pay">收货状态：</view>
					<view class="pay_info">{{info.receipt_status==2?'已收到货':'未收到货'}}</view>
				</view>
				<view class="order-info1">
					<view class="lbl_pay">申请时间：</view>
					<view class="pay_info">{{info.create_time}}</view>
				</view>
			</view>
		</view>
		<view class="footer-btn">
			<view class="order-actions">
				<uv-button text="平台介入" size="mini" :customTextStyle="{color:'#ff0000'}"></uv-button>
				<template v-if="info.status==1">
					<uv-button text="撤销售后" size="mini" @click="doSubmit"></uv-button>
				</template>
				<template v-if="info.status==3 || info.status == 4">
					<uv-button text="再次申请售后" size="mini" @click="doRefund"></uv-button>
				</template>
				<template v-if="info.status==2 || info.status == 5">
					<uv-button text="钱款去向" size="mini" @click="goMoneyPay"></uv-button>
				</template>
				<!-- <uv-button text="填写退货单" size="mini" @click="goFill"></uv-button> -->
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getGoodsAfterSalesDetail,
		cancelGoodsWithdraw
	} from '@/api'
	import {
		userefundStore
	} from '@/store'
	const refundStore = userefundStore()
	const order_info_id = ref(0)
	const remainingTime = ref(0)
	const timeData = ref('')
	const info = ref({})
	const onChange = (e) => {
		timeData.value = e
	}

	const onFinish = () => {
		//倒计时结束
	}

	const copyOrderNo = () => {
		uni.setClipboardData({
			data: info.value.order_no,
			success: function() {
				uni.showToast({
					title: '复制成功',
					icon: 'none'
				})
			}
		})
	}

	const doSubmit = async () => {
		uni.showModal({
			title: '提示',
			content: '确定要撤销本次退货退款申请吗？',
			confirmText: '确认撤销',
			confirmColor: '#ff0000',
			cancelText: '暂不撤销',
			cancelColor: '#999',
			success: function(res) {
				if (res.confirm) {
					cancelGoodsWithdraw({
						id: info.value.id
					}).then(res => {
						if (res.code == 200) {

						} else {
							uni.$uv.toast(res.msg)
						}
					})
				}
			}
		});
	}

	const doRefund = () => {
		let product = {
			id: order_info_id.value,
			product_name: info.value.product_name,
			product_image: info.value.product_image,
			spec_values: info.value.spec_values,
			price: info.value.product_price,
			cart_num: info.value.product_num,
		}
		refundStore.setRefundInfo(product)
		uni.navigateTo({
			url: '/pages-home/market/apply_refund'
		})
	}

	onLoad(async (option) => {
		order_info_id.value = option.order_info_id
		info.value = (await getGoodsAfterSalesDetail({
			order_info_id: order_info_id.value
		})).result
		let createDate = new Date(info.value.create_time)
		let endDate = new Date(createDate)
		endDate.setDate(createDate.getDate() + 7)
		let now = new Date()
		remainingTime.value = endDate - now
	})
	
	const goFill = () =>{
		uni.navigateTo({
			url: '/pages-home/market/refund_info'
		})
	}
	const goLogistics = () => {
		uni.navigateTo({
			url:'/pages-home/market/return_logistics?delivery_id='+info.value.refund_express_id+'&freight_no='+info.value.refund_express+'&refund_express_name='+info.value.refund_express_name
		})
	}
	const goMoneyPay = () => {
		uni.navigateTo({
			url: '/pages-home/market/refund_info?order_info_id='+order_info_id.value
		})
	}
</script>

<style lang="scss" scoped>
	.popup-content {
		padding: 30upx 0;

		.but {
			margin: 0 30upx;
		}
	}

	.status_tip {
		font-size: 32upx;
		font-weight: bold;
	}
	
	.f600 {
		font-weight: 600;
	}

	.f24 {
		font-size: 24upx;
	}

	.c97 {
		color: #979797;
	}

	.thumb {
		width: 100upx;
		height: 100upx;
		border-radius: 6upx;
		margin-right: 20upx;
		margin-bottom: 20upx;
	}

	.skus {
		.pName {
			font-size: 26upx;
			color: #333;
		}

		.tags {
			background: #F2F2F2;
			color: #333;
			display: inline-flex;
			border-radius: 6upx;
			font-size: 24upx;
			padding: 4upx 15upx;
		}
	}
	
	.jiage {
		margin-left: 20upx;
		margin-top: 20upx;
		.t1 {
			color: #333;
			font-size: 30upx;
			text-align: right;
		}
		
		.t2 {
			color: #979797;
			font-size: 24upx;
			text-align: right;
		}
	}

	.time {
		// #ifndef APP-NVUE
		display: flex;
		// #endif
		flex-direction: row;
		align-items: center;

		&__item {
			color: red;
			font-size: 14px;
			text-align: center;
		}
	}

	.padding2 {
		padding: 0 30upx;
	}

	.btn_comm {
		margin-top: 20upx;
		width: 100%;
		height: 80upx;
		line-height: 80upx;
		border-radius: 6upx;
		font-size: 28upx;
		background-color: #fff;
		color: #ff0000;
		border: #ff0000 1px solid;
	}

	.ass-page {
		background-color: #f7f7f7;
		min-height: 100vh;
		padding-bottom: 80px;
	}

	.margin10 {
		margin: 10px 0;
	}

	.content {
		padding-top: 10px;

		.box {
			background-color: #fff;
			padding: 10px;

			.countdown-section {
				display: flex;
				align-items: center;
				margin-bottom: 20px;
			}

			.countdown-text {
				color: #999;
				font-size: 14px;
			}

			.brand {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.brand_name {
					display: flex;
					gap: 5px;
					align-items: center;
				}
			}

			.tips {
				background-color: #F0F0F0;
				border-radius: 8px;
				padding: 10px;
				font-size: 24upx;
				margin-top: 10px;
			}

			.margin10 {
				margin-left: 10upx;
				margin-right: 10upx;
			}

			.tags {
				display: inline-flex;
			}

			.order-info {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 4px 0;
			}

			.order-info1 {
				display: flex;
				align-items: center;
				margin: 4px 0;
			}

			.orderNo {
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 24upx;
			}

			.lbl_pay {
				font-size: 24upx;
				color: #979797;
			}

			.pay_info {
				flex: 1;
				text-align: left;
				font-size: 24upx;
			}
			
			.price_row {
				display: flex;
				align-items: center;
				padding: 20upx 0;
			}
			
			.price {
				font-size: 48upx;
				font-weight: bold;
				color: #333;
			}
			
			.price1 {
				font-size: 36upx;
				font-weight: bold;
				color: #333;
			}
			
			.pay_row {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20upx 0;
				border-bottom: 1px solid #f0f0f0;
			}
			
			.pay_row:last-child {
				border-bottom: none;
			}
			
			.pay_lbl {
				font-size: 28upx;
				color: #333;
			}
			
			.pay_val {
				font-size: 28upx;
				color: #333;
			}
		}
	}

	.footer-btn {
		position: fixed;
		bottom: 20px;
		left: 0;
		right: 0;
		padding: 0 15px;

		.order-actions {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			padding: 15px;
			gap: 10px;
			background-color: #fff;
		}

	}
</style>